//
//  MyBoomViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/20.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "MyBoomViewController.h"
#import "MyBoomModel.h"
#import "MyBoomInfoViewController.h"

@interface MyBoomViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <MyBoomModel *> * boomModelArr;


@end

@implementation MyBoomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = ColorWithRGB(0xF2F2F2);
    
    [MyBoomModel myBoomModelWithSucess:^(NSArray<MyBoomModel *> *array) {
        
        self.boomModelArr = array;
    } error:^{
        
    }];
    
    [self setupNav];
    [self setupUI];
    
}

- (void)setBoomModelArr:(NSArray<MyBoomModel *> *)boomModelArr{
    
    _boomModelArr = boomModelArr;
    
    [self.tableView reloadData];
}


- (void)setupNav{
    
    self.title = @"今日热点";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}

- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT - 64) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 45 * SCREENH6;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MyBoomViewCell"];
    
    [self.view addSubview:self.tableView];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.boomModelArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:@"MyBoomViewCell"];
    
    if (cell==nil) {
        
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyBoomViewCell"];
    }
    
    MyBoomModel * model = self.boomModelArr[indexPath.row];
    
    cell.textLabel.text = model.title;
    cell.imageView.hidden = YES;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    MyBoomModel * model = self.boomModelArr[indexPath.row];
    
    MyBoomInfoViewController * infoVC = [MyBoomInfoViewController new];
    
    infoVC.model = model;
    
    [self.navigationController pushViewController:infoVC animated:YES];
}


@end
